package com.bsoft.utils;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


public class YbUtils {
    private static Logger log = LoggerFactory.getLogger(YbUtils.class);

    //工伤调用
    public Map<String, Object> doGSCallCallservice(String inputJsonStr, HttpServletRequest request) throws Exception {
        //1、调用医保业务的参数信息,四川省工伤参数
        Map<String, Object> callServiceMap = new LinkedHashMap<>();
        Map<String, Object> param = new LinkedHashMap<>();
        param.put("param", inputJsonStr);
        //serviceId
        callServiceMap.put("serviceId", "YhGsInterface");
        //method
        callServiceMap.put("method", "yh_interface_call");
        //parameters
        callServiceMap.put("parameters", param);
        log.info("工伤调用动态库请求参数：" + JSONObject.toJSONString(callServiceMap));
        //获取用户操作电脑的ip地址
        String dllUrl = "http://" + IpUtils.getIpAddr(request) + ":7711/plugin/invoke";
        log.info("调用工伤动态库IP地址为：{}", dllUrl);
        Map<String, Object> outMap = new HashMap<>();
        try {
            String outStr = HttpClientUtil.doPostYB(dllUrl, JSONObject.toJSONString(callServiceMap), "UTF-8");
            if (ObjectUtils.isEmpty(outStr)) {
                throw new RuntimeException("调用工伤医保交易接口返回空，调用失败!"+outStr);
            }
            log.info("工伤调用动态库返回参数：" + outStr);
            Map<String, Object> map = (Map<String, Object>) JSONObject.parse(outStr);
            if (!ObjectUtils.isEmpty(map.get("ResCode")) && "0".equals(map.get("ResCode").toString())) {
                JSONObject ResData = JSONObject.parseObject(map.get("ResData").toString());
                if (!ObjectUtils.isEmpty(ResData.get("int_appcode")) && "0".equals(ResData.get("int_appcode").toString())) {
                    if (!ObjectUtils.isEmpty(ResData.get("result"))) {
                        outMap = ((Map<String, Object>) (JSONObject.parseObject(ResData.get("result").toString()).get("output")));
                    }
                    outMap.put("inputJsonStr", inputJsonStr);
                } else {
                    throw new RuntimeException("调用工伤医保失败：结果为" + ResData.get("result"));
                }
            } else {
                throw new RuntimeException("调用工伤医保失败：" + map.get("ResData"));
            }
        } catch (Exception e) {
            log.error("调用医保接口出现异常：{}", e);
            throw new RuntimeException(e.getMessage());
        }
        return outMap;
    }

    public static String getReqYBPramar(Map<String,Object> commMap){
        //构造请求调用医保参数
        Map<String, Object> reqYbMap = new HashMap<>();
        reqYbMap.put("enc_type","");//加密方式 不加密传空，加密传 SM4
        reqYbMap.put("fixmedins_code",commMap.get("fixmedinsCode"));//定点医药机构编号
        reqYbMap.put("fixmedins_name",commMap.get("fixmedinsName"));//定点医药机构名称
        reqYbMap.put("inf_time", commMap.get("inf_time"));//交易时间
        reqYbMap.put("infno",commMap.get("infno"));//交易编号
        reqYbMap.put("infver","V1.0");//接口版本号
        reqYbMap.put("input",commMap.get("data"));
        reqYbMap.put("insuplc_admdvs",commMap.get("insuplcAdmdvs"));//参保地医保区划
        reqYbMap.put("mdtrtarea_admvs",commMap.get("mdtrtareaAdmvs"));//就医地医保区划
        reqYbMap.put("msgid",commMap.get("msgid"));//发送方报文 ID 定点医药机构编号(12)+时间(14)+顺序号(4) 时间格式：yyyyMMddHHmmss
        reqYbMap.put("opter",commMap.get("opterCode"));//经办人
        reqYbMap.put("opter_name",commMap.get("opterName"));//经办人姓名
        reqYbMap.put("opter_type","1");//经办人类别
        reqYbMap.put("recer_sys_code","CYHK_HIS");//接收方系统代码
        reqYbMap.put("sign_no",commMap.get("signNo"));//交易签到流水号
        reqYbMap.put("pw_ecToken","");//用于调用电子凭证相应交易保存token 使用
        //此处需要注意是否需要去签到,如果需要还要实现签到流程
        return JSON.toJSONString(reqYbMap);
    }

}
